package com.zx.idc.backend.gui.api.watch.handler;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.api.ftp.Stat;
import com.zx.idc.backend.gui.api.watch.handler.base.AbstractFtpUploadResultHandler;
import com.zx.idc.backend.gui.api.watch.pojo.FtpUploadResult;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.ds.slog.entity.SlogIllegalSite;
import com.zx.idc.ds.slog.entity.SlogIllegalSiteLog;
import com.zx.idc.ds.slog.service.ISlogIllegalSiteLogService;
import com.zx.idc.ds.slog.service.ISlogIllegalSiteService;
import com.zx.idc.xml.entity.IllegalWeb;
import com.zx.idc.xml.utils.JaxbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.zx.idc.common.attributes.FTPReportDataTypeAttributes.FTP_VIOLATION_WEBSITE_MONITORING_RECORD;

/**
 * 违法违规网站监测记录结果处理器
 */
@Component
//@Transactional(rollbackFor = Exception.class) //不需要声明事务,当前的类相当于controller
public class ViolationWebsiteMonitorRecordResultHandler extends AbstractFtpUploadResultHandler {

    @Autowired
    private ISlogIllegalSiteService iSlogIllegalSiteService;

    @Autowired
    private ISlogIllegalSiteLogService iSlogIllegalSiteLogService;

    /**
     * 匹配
     *
     * @param type
     * @return
     */
    @Override
    public boolean adapter(String type) {
        return FTP_VIOLATION_WEBSITE_MONITORING_RECORD.equals(type);
    }

    /**
     * 上报成功回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadSuccess(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("processor for monitoring records of illegal websites success start!");
        updateReportReportStatus(filePath, ReportStatusConstant.REPORTED);
        log.debug("processor for monitoring records of illegal websites success done!");
    }

    /**
     * 上报异常回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadFailed(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("processor for monitoring records of illegal websites failed start!");
        updateReportReportStatus(filePath, ReportStatusConstant.REPORTING_EXCEPTION);
        log.debug("processor for monitoring records of illegal websites failed done!");
    }

    /**
     * 更新状态
     *
     * @param filePath     文件全路径
     * @param reportStatus 上报状态
     */
    private void updateReportReportStatus(String filePath, Integer reportStatus) {
        IllegalWeb illegalWeb = JaxbUtils.xmlFileToBean(filePath, IllegalWeb.class);
        String domain = illegalWeb.getDomain();
        SlogIllegalSite slogIllegalSite = iSlogIllegalSiteService.selectFirst(
            new EntityWrapper<>(
                new SlogIllegalSite()
                    .setDomain(domain)
            ).setSqlSelect("id")
        );
        //根据域名(主键)修改上报状态
        iSlogIllegalSiteService.update(
            new SlogIllegalSite().setReportStatus(reportStatus),
            new EntityWrapper<>(
                new SlogIllegalSite().setDomain(domain)
            )
        );
        //修改日志
        if (slogIllegalSite != null) {
            iSlogIllegalSiteLogService.update(
                new SlogIllegalSiteLog().setReportStatus(reportStatus),
                new EntityWrapper<>(
                    new SlogIllegalSiteLog().setIllegalSiteId(slogIllegalSite.getId())
                )
            );
        }
    }
}
